package com.ruoyi.userr.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.userr.domain.Comments;
import com.ruoyi.userr.service.ICommentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品评论Controller
 * 
 * @author lpt 
 * @date 2025-08-18
 */
@RestController
@Controller("userCommentsController")
@RequestMapping("/userr/comments")
public class CommentsController extends BaseController
{
    @Autowired
    private ICommentsService commentsService;

    /**
     * 查询作品评论列表
     */
    @PreAuthorize("@ss.hasPermi('userr:comments:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comments comments)
    {
        startPage();
        List<Comments> list = commentsService.selectCommentsList(comments);
        return getDataTable(list);
    }

    /**
     * 导出作品评论列表
     */
    @PreAuthorize("@ss.hasPermi('userr:comments:export')")
    @Log(title = "作品评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comments comments)
    {
        List<Comments> list = commentsService.selectCommentsList(comments);
        ExcelUtil<Comments> util = new ExcelUtil<Comments>(Comments.class);
        util.exportExcel(response, list, "作品评论数据");
    }

    /**
     * 获取作品评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('userr:comments:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(commentsService.selectCommentsById(id));
    }

    /**
     * 新增作品评论
     */
    @PreAuthorize("@ss.hasPermi('userr:comments:add')")
    @Log(title = "作品评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comments comments)
    {
        return toAjax(commentsService.insertComments(comments));
    }

    /**
     * 修改作品评论
     */
    @PreAuthorize("@ss.hasPermi('userr:comments:edit')")
    @Log(title = "作品评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comments comments)
    {
        return toAjax(commentsService.updateComments(comments));
    }

    /**
     * 删除作品评论
     */
    @PreAuthorize("@ss.hasPermi('userr:comments:remove')")
    @Log(title = "作品评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(commentsService.deleteCommentsByIds(ids));
    }



    /**
     * 查询别人评论我的作品/菜谱
     * @param userId 当前用户ID（实际业务中建议从登录信息获取）
     * @return 评论列表
     */

    @GetMapping("/my")
    public ResponseEntity<List<Comments>> getCommentsForMe(@RequestParam("userId") Integer userId) {
        Long userIdLong = Long.valueOf(userId);
        List<Comments> comments = commentsService.getCommentsForMe(userIdLong);
        return ResponseEntity.ok(comments);
    }

}
